<template>
    <div>
        <Modal v-model="resetPasswordManager.dialogVisible"
                title="重置密码"
                @on-cancel="closeDialog"
                :mask-closable="false">
            <i-form ref="newModel" :model="newModel" :rules="rules">
                <Form-item prop="newPassword">
                    <i-input type="password" v-model="newModel.newPassword" placeholder="请输入新密码"></i-input>
                </Form-item>
            </i-form>

            <template slot="footer">
                <Row type="flex">
                    <i-col span="24">
                        <div class="pull-right">
                            <i-button type="ghost" @click="closeDialog">取消</i-button>
                            <i-button type="primary" @click="saveModel" :loading="isLoading">确定</i-button>
                        </div>
                    </i-col>
                </Row>
            </template>
        </Modal>
    </div>
</template>
<script>
    export default {
      props: {
        resetPasswordManager: {
          type: [Array, Object],
          default: []
        }
      },
      data () {
        const validatePassword = (rule, value, callback) => {
          console.log(value)
          if (value.length < 6 || value.length > 25) {
            callback(new Error('密码长度应为6-25个字符！'))
          } else {
            callback()
          }
        }
        return {
          newModel: {
            newPassword: ''
          },
          isLoading: false,
          rules: {
            newPassword: [
              {required: true, validator: validatePassword, trigger:'blur'}
            ]
          }
        }
      },
      methods: {
        closeDialog: function () {
          this.resetPasswordManager.dialogVisible = false;
          this.newModel.newPassword = '';
          this.$refs['newModel'].resetFields();
        },
        saveModel: function () {
          let self = this;
          self.$refs['newModel'].validate((valid) => {
            if (valid) {
              console.log('表单验证通过');
              self.isLoading = true;
              axios.post('/api/admin/reset-password/' + self.resetPasswordManager.userId, {
                password: self.newModel.newPassword
              }).then(function (response) {
                self.$Modal.remove();
                self.$Notice.success({
                  title: '重置成功',
                  desc: self.resetPasswordManager.userName + '已重置密码'
                });
                self.closeDialog();
              }).catch(function (error) {
                console.log(error);
                self.isLoading = false;
              })

            } else {
              console.log('表单验证失败');
            }
          });
        }
      },
    }
</script>